﻿using System.Windows;
using GraphShape.Controls;

namespace KinonekoSoftware.Foundation.GraphShape.Windows
{
    public class GraphLayoutHelper : AvaloniaObject
    {
        public static readonly AttachedProperty<bool>        UseMethodProperty;
        public static readonly AttachedProperty<GraphLayout> LayoutControlProperty;
        public static readonly AttachedProperty<string>      LayoutMethodProperty;
        
        static GraphLayoutHelper()
        {
            UseMethodProperty     = AvaloniaProperty.RegisterAttached<GraphLayoutHelper, MenuItem, bool>("UseMethod");
            LayoutControlProperty = AvaloniaProperty.RegisterAttached<GraphLayoutHelper, MenuItem, GraphLayout>("LayoutControl");
            LayoutMethodProperty  = AvaloniaProperty.RegisterAttached<GraphLayoutHelper, MenuItem, string>("LayoutMethod");
            UseMethodProperty.Changed.Subscribe(args =>
            {
                
                if (args.Sender is MenuItem control)
                {
                    var method = GetLayoutMethod(control);
                    var layout = GetLayoutControl(control);

                    if (layout is null || string.IsNullOrEmpty(method))
                    {
                        return;
                    }
                    
                    layout.LayoutAlgorithmType = method;
                }
                
            });
        }
        
        
        public static string GetLayoutMethod(MenuItem element)
        {
            return element.GetValue(LayoutMethodProperty);
        }

        
        public static void SetLayoutMethod(MenuItem element, string value)
        {
            element.SetValue(LayoutMethodProperty, value);
        }

        public static void SetUseMethod(MenuItem element, bool value)
        {
            
            element.SetValue(UseMethodProperty, value);
        }

        public static bool GetUseMethod(MenuItem element)
        {
            return element.GetValue(UseMethodProperty);
        }

        public static void SetLayoutControl(MenuItem element, GraphLayout value)
        {
            element.SetValue(LayoutControlProperty, value);
        }

        public static GraphLayout GetLayoutControl(MenuItem element)
        {
            return element.GetValue(LayoutControlProperty);
        }
    }
}